package com.wgworkshop.games.raindrop;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

import java.util.Iterator;

/**
 * Created by wlc on 2017/5/27.
 */
public class GameScreen extends ScreenAdapter {
    private final Raindrop game;
    private OrthographicCamera camera;

    private Texture raindropImage;
    private Texture bucketImage;

    private Sound dropSound;
    private Music rainMusic;

    private Rectangle bucket;
    private Array<Rectangle> raindrops;

    private long lastDropTime;

    private Vector3 touchPos;

    private int dropsGathered;

    private final int MOVE_SPEED = 200;
    private final long SPAWN_SPEED = 1000000000;

    private final int BUCKET_SIZE = 64;
    private final int RAINDROP_SIZE = 64;

    private Cursor cursor;

    public GameScreen(final Raindrop game) {
        this.game = game;

        camera = new OrthographicCamera();
        camera.setToOrtho(false, game.WIDTH, game.HEIGHT);

        raindropImage = new Texture(Gdx.files.internal("drop.png"));
        bucketImage = new Texture(Gdx.files.internal("bucket.png"));

        dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
        rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
        rainMusic.setLooping(true);

        bucket = new Rectangle();
        bucket.x = (game.WIDTH - BUCKET_SIZE) / 2;
        bucket.y = 20;
        bucket.width = BUCKET_SIZE;
        bucket.height = BUCKET_SIZE;

        raindrops = new Array<>();
        spawnRaindrop();

        touchPos = new Vector3();

        cursor=Gdx.graphics.newCursor(new Pixmap(Gdx.files.internal("cursor.png")),0,0);
    }

    @Override
    public void show() {
        rainMusic.play();
        Gdx.graphics.setCursor(cursor);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();

        game.batch.setProjectionMatrix(camera.combined);

        game.batch.begin();
        game.font.draw(game.batch, "Raindrops collected:" + dropsGathered, 0, game.HEIGHT);
        game.batch.draw(bucketImage, bucket.x, bucket.y);
        for (Rectangle raindrop : raindrops) {
            game.batch.draw(raindropImage, raindrop.x, raindrop.y);
        }
        game.batch.end();

        if (Gdx.input.isTouched()) {
            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(touchPos);
            bucket.x = touchPos.x - bucket.width / 2;
        }

        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            bucket.x -= MOVE_SPEED * Gdx.graphics.getDeltaTime();
            if (bucket.x < 0) {
                bucket.x = 0;
            }
        }

        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            bucket.x += MOVE_SPEED * Gdx.graphics.getDeltaTime();
            float max = game.WIDTH - bucket.width;
            if (bucket.x > max) {
                bucket.x = max;
            }
        }

        if (TimeUtils.nanoTime() - lastDropTime > SPAWN_SPEED) {
            spawnRaindrop();
        }

        Iterator<Rectangle> iterator = raindrops.iterator();
        while (iterator.hasNext()) {
            Rectangle raindrop = iterator.next();
            raindrop.y -= MOVE_SPEED * Gdx.graphics.getDeltaTime();
            if (raindrop.y + raindrop.height < 0) {
                iterator.remove();
            }

            if (raindrop.overlaps(bucket)) {
                dropSound.play();
                dropsGathered++;
                iterator.remove();
            }
        }
    }

    @Override
    public void dispose() {
        if (dropsGathered > game.highScore) {
            game.highScore = dropsGathered;
        }

        raindropImage.dispose();
        bucketImage.dispose();
        dropSound.dispose();
        rainMusic.dispose();

        cursor.dispose();
    }

    private void spawnRaindrop() {
        Rectangle raindrop = new Rectangle();
        raindrop.x = MathUtils.random(0, game.WIDTH - RAINDROP_SIZE);
        raindrop.y = game.HEIGHT;
        raindrop.width = RAINDROP_SIZE;
        raindrop.height = RAINDROP_SIZE;
        raindrops.add(raindrop);
        lastDropTime = TimeUtils.nanoTime();
    }
}
